package com.ahuan.controller;


import com.ahuan.domain.Dept;
import com.ahuan.domain.Role;
import com.ahuan.domain.User;
import com.ahuan.exception.BusinessException;
import com.ahuan.resp.Res;
import com.ahuan.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/api/User")
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping("/list")
    public Res getUserList(Integer page, Integer limit) {
        try {
            List<User> users = userService.queryByPage(page, limit);
            log.info("users:{}", users);
            if (users.size() > 0) {
                return Res.successOperate(users, users.size());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("获取用户列表失败");
        }
        return Res.failOperate();
    }


    @PostMapping("/save")
    public Res addUser(User user) {
        try {
            String userId = MDC.get("userId");
            user.setEditUser(userId);
            user.setAddUser(userId);
            log.info("save user:{}", user);
            MDC.clear();
            if (user.getUserId() == null || user.getUserId().equals("")) {
                boolean addRet = userService.addUser(user);
                if (addRet) {
                    return Res.successOperate();
                }
                return Res.failOperate();
            } else {
                boolean editRet = userService.editUser(user, userId);
                if (editRet) {
                    return Res.successOperate();
                }
                return Res.failOperate();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("保存用户失败");
        }
    }

    @PostMapping("/pwd")
    public Res resetPwd(String userId) {
        try {
            log.info("ResetPwd UserId:{}", userId);
            boolean b = userService.resetPwd(userId);
            if (b) {
                return Res.successOperate();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("重置密码失败");
        }
        return Res.failOperate();
    }

    @DeleteMapping("/delete")
    public Res deleteUser(@RequestParam("ids") String userId) {
        try {
            log.info("ResetPwd UserId:{}", userId);
            boolean b = userService.deleteUser(userId);
            if (b) {
                return Res.successOperate();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("删除用户失败");
        }
        return Res.failOperate();
    }

    @GetMapping("/roleList")
    public Res getRoleList() {
        try {
            log.info("查询用户角色列表");
            List<Role> list = userService.queryRoleList();
            log.info("查询到的角色列表为:{}", list);
            if (list != null && list.size() > 0) {
                return Res.successOperate(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("查询用户角色列表失败");
        }
        return Res.failOperate();
    }

    @GetMapping("/deptList")
    public Res getDeptList() {
        try {
            log.info("查询用户部门列表");
            List<Dept> list = userService.queryDeptList();
            log.info("查询到的角色列表为:{}", list);
            if (list != null && list.size() > 0) {
                return Res.successOperate(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("查询用户部门列表失败");
        }
        return Res.failOperate();
    }
}


